#include <assert.h>
#include <stdarg.h>
#include <wchar.h>
#include "TestFactory.h"
#include "DebugXcode.h"

namespace AllTing
{

namespace Test
{

DebugXcode::~DebugXcode()
{
}


UInt32 DebugXcode::Log(Char* pszFormat, ...) const
{
	va_list args;
	va_start(args, pszFormat);

	UInt32 nLen = vswprintf(szBuffer_, DEBUG_XCODE_MAX_LOG_BUFFER, pszFormat, args) + sizeof(Char);

	szBuffer_[nLen] = 0x00;
	
	UInt32 nRet = Debug::Log((const Char*)szBuffer_);

	va_end(args);

	return nRet;
}

Bool DebugXcode::Report() const
{
	TestConstIterator itStart = TestFactory::Instance()->BeginTest();
	TestConstIterator itEnd = TestFactory::Instance()->EndTest();

	UInt32 nTestCount = TestFactory::Instance()->Size();
	UInt32 nFailedTest = 0;
	UInt32 nFailedAssert = 0;

	for(; itStart!=itEnd; ++itStart)
	{
		AssertConstIterator itAssertStart = (*itStart)->BeginFailedAssert();
		AssertConstIterator itAssertEnd = (*itStart)->EndFailedAssert();

		if(itAssertStart != itAssertEnd)
			++nFailedTest;

		for(; itAssertStart!=itAssertEnd; ++itAssertStart)
		{
			if(False == itAssertStart->strFile.empty() && 0 < itAssertStart->nLine)
			{
				Log(L"%s(%u) : Failed %s test\n", 
						itAssertStart->strFile.c_str(), itAssertStart->nLine, (*itStart)->Name());
				++nFailedAssert;
			}
		}
	}

	Log(L"\r\n============================================================================\r\n");
	Log(L"Done - %u tests(%u asserts), %u succeeded(%u asserts), %u failed(%u asserts)", 
				nTestCount, nAssert_, nTestCount-nFailedTest, nAssert_-nFailedAssert, nFailedTest, nFailedAssert);
	Log(L"\r\n============================================================================\r\n");

	return True;
}


};	// namespace Test

}	// namespace AllTing